#!/usr/bin/env perl
use SOAP::Lite;
use Data::Dumper;

$wsdl = 'http://soap.genome.jp/KEGG.wsdl';

$serv = SOAP::Lite -> service($wsdl);

open (EC,$ARGV[0]) or die "Cannot open $ARGV[0]";

while (my $line=<EC>) {
    chomp $line;
    my ($map,undef,undef,undef,$ecs,undef,undef,undef,undef,$allecs) = split ("\t",$line);
    my @ecs= split (";",$ecs);	
    my @allecs= split (";",$allecs);
	my $tmpec=();
	my $tmpallec=();
	
	if (scalar(@ecs) > 0){   
		if (scalar(@allecs) > 0){
		foreach $allec(@allecs) {
			push (@{$tmpallec},"ec:".$allec);
		}
		
		my $econ=();
		foreach my $ectmp(@ecs){
			$econ->{$ectmp}=1;
	    }
		
		$genes = SOAP::Data->type(array => $tmpallec);
		
		my $bg_color1 = '#FFFACD'; #color para enzima en salmon
		my $fg_color1 = '#000000'; #color para no cambio de expresion
		my $fg_color2 = '#ff0000'; #color para cambio de expresion
		
		my $bg_listtmp = (); 
		my $fg_listtmp = ();
		
		foreach my $ectmp(@allecs){
		push (@{$bg_listtmp},$bg_color1);
		
		if(defined $econ->{$ectmp}){
			push (@{$fg_listtmp},$fg_color2);
		}else{
			push (@{$fg_listtmp},$fg_color1);
		}
		}
		
		my $fg_list = SOAP::Data->type(array => $fg_listtmp);
		my $bg_list = SOAP::Data->type(array => $bg_listtmp);
		
		#print Dumper($fg_list);
		#print Dumper($bg_list);
	
#		fg_list  = [ '#ff0000', '#f00000' ]
#		bg_list  = [ '#ffff00',  ]
#		color_pathway_by_elements('path:bsu00010', element_id_list, fg_list, bg_list)
		
	
	$result = $serv->color_pathway_by_objects("path:$map", $genes, $fg_list, $bg_list);

	print $result."\n";
	`wget $result -O ss$map.png`; # URL of the generated image
		}
	}
}


#print Dumper($genes);


=cut

my $serv = SOAP::Lite -> service("http://soap.genome.jp/KEGG.wsdl");
my $genes = SOAP::Data->type(array => ['eco:b0002', 'cpd:C00263']);

my $result = $serv->mark_pathway_by_objects('path:eco00260', $genes);
print Dumper($result)."\n";

=cut
sub SOAP::Serializer::as_ArrayOfstring{
  my ($self, $value, $name, $type, $attr) = @_;
  return [$name, {'xsi:type' => 'array', %$attr}, $value];
}

sub SOAP::Serializer::as_ArrayOfint{
  my ($self, $value, $name, $type, $attr) = @_;
  return [$name, {'xsi:type' => 'array', %$attr}, $value];
}
